#!/usr/bin/env python
import cgi
import datetime
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp

Out =r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Your code</title>
	<script type="text/javascript" src="scripts/shCore.js"></script>
	<script type="text/javascript" src="scripts/shBrushBash.js"></script>
	<script type="text/javascript" src="scripts/shBrushCpp.js"></script>
	<script type="text/javascript" src="scripts/shBrushCSharp.js"></script>
	<script type="text/javascript" src="scripts/shBrushCss.js"></script>
	<script type="text/javascript" src="scripts/shBrushDelphi.js"></script>
	<script type="text/javascript" src="scripts/shBrushDiff.js"></script>
	<script type="text/javascript" src="scripts/shBrushGroovy.js"></script>
	<script type="text/javascript" src="scripts/shBrushJava.js"></script>
	<script type="text/javascript" src="scripts/shBrushJScript.js"></script>
	<script type="text/javascript" src="scripts/shBrushPhp.js"></script>
	<script type="text/javascript" src="scripts/shBrushPlain.js"></script>
	<script type="text/javascript" src="scripts/shBrushPython.js"></script>
	<script type="text/javascript" src="scripts/shBrushRuby.js"></script>
	<script type="text/javascript" src="scripts/shBrushScala.js"></script>
	<script type="text/javascript" src="scripts/shBrushSql.js"></script>
	<script type="text/javascript" src="scripts/shBrushVb.js"></script>
	<script type="text/javascript" src="scripts/shBrushXml.js"></script>
	<link type="text/css" rel="stylesheet" href="styles/shCore.css"/>
	<link type="text/css" rel="stylesheet" href="styles/shThemeDefault.css"/>
	<script type="text/javascript">
		SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
		SyntaxHighlighter.all();
	</script>
</head>

<body >
<h1>Here is your Code</h1>
<p>Good Luck!</p>

<pre class="brush: %s;">
%s
</pre>
</html>
"""


class CodeStr(db.Model):
  content = db.TextProperty()
  date = db.DateTimeProperty(auto_now_add=True)
  lang = db.StringProperty(multiline=False)

class InputCode(webapp.RequestHandler):
  def get(self):
    self.response.out.write('<html><body>')
    self.response.out.write('Code:')

    self.response.out.write("""<form action="/sc" method="post">
                                    <div><select size="4" id= "lang" name="lang">
                                        <option>python</option>
                                        <option>js</option>
                                        <option>xml</option>
                                        <option>bash</option>
                                    </select></div>
                                    <div><textarea name="content" rows="8" cols="70"></textarea></div>
                                    <div><input type="submit" value="Submit"></div>
                                </form>
                            </body>""")


class SaveCode(webapp.RequestHandler):
  def post(self):
    self.code = CodeStr()
    self.code.content = self.request.get('content')
    self.code.lang = self.request.get('lang')
    self.code.put()
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Your code\'s ID is %s\n' % self.code.key().id())
    self.response.out.write('You can visit http://billgaesh.appspot.com/o?id=%s to get your code\n' % self.code.key().id())
    self.response.out.write('Good Luck')
  def get(self):
    self.response.out.write('Sorry This doesn\'t work!')

class OutCode(webapp.RequestHandler):
  def get(self):
    global Out
    id = self.request.get('id')
    key = db.Key.from_path("CodeStr", int(id))
    query = CodeStr.all()
    query.ancestor(key)
    code = query.get()
    s = code.content.replace('<', '&lt;').replace('>', '&gt;')
    O = Out % (code.lang, s)
    self.response.out.write(O)

application = webapp.WSGIApplication([
  ('/i', InputCode),
  ('/sc', SaveCode),
  ('/o', OutCode)
], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
